<?php

class edit_champsignups
{
	var $champid;
	var $teamid;
	var $carid;
	var $driverid;
	var $divisionid;
	var $carNo;
	var $teamOptions;
	var $carOptions;
	var $driverOptions;
	var $divisionOptions;
	
	var $add_record;
	var $valid;

	function edit_champsignups($add_record, $champid, $vehicles, $divisions, $record='')
	{
		global $db, $lang;
        
        
        
		if($add_record)
		{
			$this->add_record = true;
			$this->carNo = 0;
			$this->teamid = 0;
			$this->carid = 0;
			$this->driverid = 0;
			$this->divisionid = 0;
			$this->champid = $champid;
			
			$this->driverOptions = '';
			$q_drivers = 'SELECT u.user_id, u.username, p.pf_drivername 
						  FROM ' . USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g, '. DRIVERNAME_TABLE . ' p  
						  WHERE p.pf_drivername != ""  AND g.user_id = u.user_id AND g.group_id = 3 AND u.user_id = p.user_id AND u.user_id NOT IN ' .
					'(SELECT cs_driver FROM ' . CHAMPSIGNUPS_TABLE . ' WHERE cs_champ = ' . $this->champid . ') ORDER BY p.pf_drivername ASC';	

			$result = $db->sql_query($q_drivers);
			while($row = $db->sql_fetchrow($result)) 
			{
				$this->driverOptions = $this->driverOptions . 
						'<option value="'.$row['user_id'].'" '. ($this->driverid==$row['user_id'] ? 'SELECTED' : '').'>'  . $row['pf_drivername'] . 
											' (' . $row['username'] . ')</option>';
			}
			
			$carOptions = '<option value="0" ' . ($this->carid==0 ? 'SELECTED' : '') . '>' . $lang['Unassigned'] . '</option>';
    		foreach( $vehicles as $key => $value){
                $carOptions = $carOptions . 
						'<option value="'.$value['c_id'].'" '. ($this->carid == $value['c_id'] ? 'SELECTED' : '').'>'  .
                            $value['c_manufacturer'] . ' ' . $value['c_model'] . '</option>';
    		}
            $this->carOptions = $carOptions;
			
			
			
			
			$teamOptions = '<option value="0" ' . ($this->teamid==0 ? 'SELECTED' : '') . '>' . $lang['Independent'] . '</option>';
			$q_teams = 'SELECT t_id, t_name FROM ' . TEAMS_TABLE . ', ' . TEAMMEMBERS_TABLE . ' WHERE (tm_team = t_id OR tm_team = t_parent) AND tm_approved = 1 AND tm_driver = ' . 
							$this->driverid . ' ORDER BY t_name';
			$result = $db->sql_query($q_teams);
			while($row = $db->sql_fetchrow($result)) 
			{
				$teamOptions = $teamOptions . 
						'<option value="'.$row['t_id'].'" '. ($this->teamid==$row['t_id'] ? 'SELECTED' : '').'>'  . $row['t_name'] . '</option>';
			}
			$this->teamOptions = $teamOptions;
			
			
			$divisionOptions = '<option value="0" ' . ($this->divisionid==0 ? 'SELECTED' : '') . '>' . 
                                        $lang['Unassigned'] . '</option>';
    		foreach( $divisions as $key => $value){
                $divisionOptions = $divisionOptions . 
						'<option value="'.$value['d_id'].'" '. ($this->divisionid==$value['d_id'] ? 'SELECTED' : '').'>'  . 
                                $value['d_title'] . '</option>';
    		}
			$this->divisionOptions = $divisionOptions;
			
			$this->valid = true;
			
		}
		else if(!$add_record)
		{
			$this->add_record = false;
			$this->driverid = $record['cs_driver'];
			$this->carNo = $record['cs_no'];
			$this->teamid = $record['cs_team'];
			$this->carid = $record['cs_car'];
			$this->divisionid = $record['cs_div'];
			$this->champid = $champid;
			
            $driverOptions = '<option value="'.$record['user_id'].'" SELECTED>'  . $record['user_drivername'] . 
                                        ' (' . $record['username'] . ')</option>';
			$this->driverOptions = $driverOptions;
			
			
			$carOptions = '<option value="0" ' . ($this->carid==0 ? 'SELECTED' : '') . '>' . $lang['Unassigned'] . '</option>';
    		foreach( $vehicles as $key => $value){
                $carOptions = $carOptions . 
						'<option value="'.$value['c_id'].'" '. ($this->carid == $value['c_id'] ? 'SELECTED' : '').'>'  .
                            $value['c_manufacturer'] . ' ' . $value['c_model'] . '</option>';
    		}
            $this->carOptions = $carOptions;
			
			
			
			
			$teamOptions = '<option value="0" ' . ($this->teamid==0 ? 'SELECTED' : '') . '>' . $lang['Independent'] . '</option>';
			$q_teams = 'SELECT t_id, t_name FROM ' . TEAMS_TABLE . ', ' . TEAMMEMBERS_TABLE . ' WHERE  (tm_team = t_id OR tm_team = t_parent) AND tm_approved = 1 AND tm_driver = ' . 
							$this->driverid . ' ORDER BY t_name';
			$result = $db->sql_query($q_teams);
			while($row = $db->sql_fetchrow($result)) 
			{
				$teamOptions = $teamOptions . 
						'<option value="'.$row['t_id'].'" '. ($this->teamid==$row['t_id'] ? 'SELECTED' : '').'>'  . $row['t_name'] . '</option>';
			}
			$this->teamOptions = $teamOptions;
			
			$divisionOptions = '<option value="0" ' . ($this->divisionid==0 ? 'SELECTED' : '') . '>' . 
                                        $lang['Unassigned'] . '</option>';
    		foreach( $divisions as $key => $value){
                $divisionOptions = $divisionOptions . 
						'<option value="'.$value['d_id'].'" '. ($this->divisionid==$value['d_id'] ? 'SELECTED' : '').'>'  . 
                                $value['d_title'] . '</option>';
    		}
			$this->divisionOptions = $divisionOptions;
			
			$this->valid = true;
		}
		else
		{
			$this->valid = false;
		}
        
	}
		
	function render()
	{		
		global $skin_dir, $lang;
		
		$submitText = $this->add_record ? $lang['AddButton'] : $lang['EditButton'];
		$submitValue = $this->add_record ? 'addRecord' : 'editRecord';
		$remS = $this->add_record ? '<!--' : '';
		$remE = $this->add_record ? '-->' : '';
				
		$champid = $this->champid;
		$carNo = $this->carNo;
						
		$teamOptions = $this->teamOptions;
		$carOptions = $this->carOptions;
		$driverOptions = $this->driverOptions;
		$divisionOptions = $this->divisionOptions;
			 
    	include $skin_dir . 'admin/editchampsignup.tpl';
	}
}
?>